package com.distributed.common;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;



public class User {
	private Map<String, String> info = new HashMap<String, String>();
	
	public User() {
		try {
			InetAddress address = InetAddress.getLocalHost();
			info.put(Constant.DATA_IP, address.getHostName());
			
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	}
	
	public User(String username, String password, int port, int timestamp) {
		this();
		setUserName(username);
		setPassword(password);
		setPort(port);
		setTimeStamp(timestamp);
	}
	
	public User(String username, String password, int port, int timestamp, String tablename) {
		this(username, password, port, timestamp);
		setTableName(tablename);
	}
	
	public void setUserName(String username) {
		info.put(Constant.DATA_USERNAME, username);
	}
	
	public String getUserName() {
		return info.get(Constant.DATA_USERNAME);
	}
	
	public void setPassword(String password) {
		info.put(Constant.DATA_PASSWORD, password);
	}
	
	public String getPassword() {
		return info.get(Constant.DATA_PASSWORD);
	}
	
	public String getIp() {
		return info.get(Constant.DATA_IP);
	}
	
	public void setIp(String ip) {
		info.put(Constant.DATA_IP, ip);
	}
	
	public void setPort(int port) {
		info.put(Constant.DATA_PORT, String.valueOf(port));
	}
	
	public void setTimeStamp(int timestamp) {
		info.put(Constant.DATA_TIMESTAMP, String.valueOf(timestamp));
	}
	
	public int getTimeStamp() {
		String timestamp_s = info.get(Constant.DATA_TIMESTAMP);
		int timestamp = -1;
		try {
			timestamp = Integer.valueOf(timestamp_s);
			return timestamp;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		return -1;
	}
	
	public int getPort() {
		String port_s = info.get(Constant.DATA_PORT);
		if (port_s == null || "".equals(port_s)) {
			return -1;
		}
		try {
			int port = Integer.valueOf(port_s);
			return port;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		
		return -1;
	}
	
	public String getTableName() {
		return info.get(Constant.DATA_TABLE_NAME);
	}
	
	public void setTableName(String tablename) {
		info.put(Constant.DATA_TABLE_NAME, tablename);
	}
	
	public int getVerify() {
		String verify_s = info.get(Constant.DATA_VERIFY);
		int verify = 0;
		try {
			verify = Integer.valueOf(verify_s);
			return verify;
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return verify;
	}
	
	public void setVerify(int verify) {
		info.put(Constant.DATA_VERIFY, String.valueOf(verify));
	}
	
	public String toString() {
		return toJSON().toString();
	}
	
	public static User fromString(String user_s) {
		JSONObject jsonobject = JSONObject.fromObject(user_s);
		if (jsonobject == null) {
			return null;
		}
		return fromJSON(jsonobject);
	}
	
	private JSONObject toJSON() {
		JSONObject jsonobject = new JSONObject();
		
		String userName = getUserName();
		if (userName != null && "".equals(userName)) {
			jsonobject.put(Constant.DATA_USERNAME, userName);
		}
		
		String password = getPassword();
		if (password != null && "".equals(password)) {
			jsonobject.put(Constant.DATA_PASSWORD, password);
		}
		
		String ip = getIp();
		if (ip != null && "".equals(ip)) {
			jsonobject.put(Constant.DATA_IP, ip);
		}
		
		String port = String.valueOf(getPort());
		if (port != null && "".equals(port)) {
			jsonobject.put(Constant.DATA_PORT, port);
		}
		
		String timestamp = String.valueOf(getTimeStamp());
		if (timestamp != null && "".equals(timestamp)) {
			jsonobject.put(Constant.DATA_TIMESTAMP, timestamp);
		}
		
		String tableName = getTableName();
		if (tableName != null && "".equals(tableName)) {
			jsonobject.put(Constant.DATA_TABLE_NAME, tableName);
		}
		
		return jsonobject;
	}
	
	private static User fromJSON(JSONObject jsonobject) {
		if (jsonobject == null) {
			return null;
		}
		String userName = jsonobject.getString(Constant.DATA_USERNAME);
		String password = jsonobject.getString(Constant.DATA_PASSWORD);
		String ip = jsonobject.getString(Constant.DATA_IP);
		String port_s = jsonobject.getString(Constant.DATA_PORT);
		int port = -1;
		try {
			port = Integer.valueOf(port_s);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		String timestamp_s = jsonobject.getString(Constant.DATA_TIMESTAMP);
		int timestamp = -1;
		try {
			timestamp = Integer.valueOf(timestamp_s);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		String tableName = jsonobject.getString(Constant.DATA_TABLE_NAME);
		User user = new User(userName, password, port, timestamp, tableName);
		user.setIp(ip);
		return user;
	}
}
